﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Log;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TZ.PlugIn.FileSrv
{
    [Description("采购订单获取查看电子签约链接")]
    [Kingdee.BOS.Util.HotUpdate]

    public class PurOrderGetViewUrl : AbstractBillPlugIn
    {
        public override void BarItemClick(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.EqualsIgnoreCase("tbGetViewUrl"))
            {
                var FContractId = "";
                if (!this.View.Model.GetValue("FContractId").IsNullOrEmpty())
                    FContractId = this.View.Model.GetValue("FContractId").ToString().Trim();
                //if (FContractId == "")
                //{
                //    this.View.ShowMessage("没有创建电子签约单");
                //    return;
                //}
                var ViewUrl = "";
                if (!this.View.Model.GetValue("FViewUrl").IsNullOrEmpty())
                    ViewUrl = this.View.Model.GetValue("FViewUrl").ToString().Trim();

                var BillNo = this.View.Model.GetValue("FBillNo").ToString();
                var ErrMsg = "";
                var isPost = true;
                Common.GetParams(this.Context);
                var Url = string.Format(Common.ViewBillUrl, FContractId);
                var result = Common.GetView(Url, Common.AppToken, Common.AppSecret, ref isPost);

                var JsonResult = result as JObject;
                if (JsonResult["code"].ToString() != "0")
                {
                    ErrMsg = "code【" + JsonResult["code"].ToString().Trim() + "】,message【" + JsonResult["message"].ToString().Trim() + "】";
                    this.View.ShowErrMessage(ErrMsg);

                    // 写上机操作日志
                    var logs = new List<LogObject>();
                    var log = new LogObject();
                    log.pkValue = "";
                    log.Description = $"获取查看链接失败:+{result}";
                    log.OperateName = $"获取查看链接失败:{Url},{isPost}";
                    log.ObjectTypeId = "";
                    log.SubSystemId = "";
                    log.Environment = OperatingEnvironment.BizOperate;
                    logs.Add(log);
                    LogServiceHelper.BatchWriteLog(this.Context, logs);
                }
                else
                {
                    ViewUrl = JsonResult["viewUrl"].ToString();
                    var sql = string.Format("update T_PUR_POORDER set FVIEWURL = '{1}' where FBILLNO = '{0}'", BillNo, ViewUrl);
                    DBUtils.Execute(this.Context, sql);
                    this.View.AddAction("ShowWebURL", ViewUrl);

                    // 写上机操作日志
                    var logs = new List<LogObject>();
                    var log = new LogObject();
                    log.pkValue = "";
                    log.Description = $"获取查看链接成功:{result}";
                    log.OperateName = $"获取查看链接成功:{Url},{isPost}";
                    log.ObjectTypeId = "";
                    log.SubSystemId = "";
                    log.Environment = OperatingEnvironment.BizOperate;
                    logs.Add(log);
                    LogServiceHelper.BatchWriteLog(this.Context, logs);
                }

                //2025-9-18 注释 不判断 直接打开链接
                //if (!isPost)
                //{
                //    ErrMsg = result.ToString().Trim();
                //    this.View.ShowErrMessage(ErrMsg);
                //}
                //else
                //{
                //    var JsonResult = result as JObject;
                //    if (JsonResult["code"].ToString() != "0")
                //    {
                //        ErrMsg = "code【" + JsonResult["code"].ToString().Trim() + "】,message【" + JsonResult["message"].ToString().Trim() + "】";
                //        this.View.ShowErrMessage(ErrMsg);
                //    }
                //    else
                //    {
                //        ViewUrl = JsonResult["viewUrl"].ToString();
                //        var sql = string.Format("update T_PUR_POORDER set FVIEWURL = '{1}' where FBILLNO = '{0}'", BillNo, ViewUrl);
                //        DBUtils.Execute(this.Context, sql);
                //        this.View.AddAction("ShowWebURL", ViewUrl);
                //    }
                //}

                //第一次注释
                //if (ViewUrl.Trim() == "")
                //{

                //}
                //else
                //    this.View.AddAction("ShowWebURL", ViewUrl);
            }
        }
    }
}
